from django.db import models


# Create your models here.
# 写一个继承models.Model的类
class UserInfo(models.Model):
    name = models.CharField(max_length=32)
    pwd = models.CharField(max_length=64)
    age = models.IntegerField()


class StudentInfo(models.Model):
    name = models.CharField(max_length=32)
    height = models.IntegerField()
    weight = models.IntegerField(default=100)
    age = models.IntegerField()


# 自定义JSON编码器
def student_info_encoder(obj):
    if isinstance(obj, StudentInfo):
        model_dict = {}
        for field in obj._meta.fields:
            model_dict[field.attname] = getattr(obj, field.attname)
        return model_dict  # 返回新字典
    raise TypeError(f"{obj} is not JSON serializable")
